/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.chartextensions.mutablename;

import JFreeChartExtensions.chartextensions.AbstractChartPanelExtension;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.JChartMenu.JChartMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartPanel;

/**
 *
 * @author Nobody
 */
public class MutableNameExtension extends AbstractChartPanelExtension {

    private JChartMenu menu;

    public MutableNameExtension(ExtendableChartPanel panel) {
        super(panel);
        name = "MutableNameExtension";
    }

    @Override
    public void applyExtension(ChartPanel chartPanel) {
        this.chartPanel = (ExtendableChartPanel) chartPanel;
        final ExtendableChartPanel extendableChartPanel = (ExtendableChartPanel) chartPanel;

        menu = new JChartMenu("Change title");
        menu.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String name = JOptionPane.showInputDialog(extendableChartPanel,
                        "Change chart Title",
                        extendableChartPanel.getName());

                if (name != null) {
                    extendableChartPanel.getParentContainer().setTitle(name);
                }
                extendableChartPanel.getChart().setTitle(name);
                extendableChartPanel.updateUI();
            }
        });
        chartPanel.getPopupMenu().add(menu, 1);
    }

    @Override
    public void removeExtension() {
        chartPanel.getPopupMenu().remove(1);
    }
}
